<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Project extends CI_Controller {
	public $variable = "filename";
	
	public function __construct(){
		parent::__construct ();
		if(!$this->session->userdata('loggedin')) redirect('main', 'refresh');
		$this->lang->load('site', get_lang());
	}
	
	function index()
	{
		
		$config = $this->input->post();
		
		$types='';
		foreach ($config["file_type"] as $item){
			$types.= $item.',';
		}
		$config["file_type"] = substr($types, 0, -1);

		$this->load->model('ProjectModel');
		$this->load->library('form_validation');
		$this->load->helper(array('form', 'url', 'language', 'date'));
		
		$this->form_validation->set_rules('segment', 'Segmento', 'required');
		$this->form_validation->set_rules('file_type', 'Formato de arquivo', 'required');
		$this->form_validation->set_rules('title', 'Password Confirmation', 'required');
		$this->form_validation->set_rules('value', 'Valor', 'required');
		$this->form_validation->set_rules('description', 'Descriçãp', 'required');

		if ($this->form_validation->run() == FALSE)
		{
			//Corrigir aqui pois so deve entrar aqui se o formulário der erro e esta entrando sempre!
			$last_project = $this->ProjectModel->save($config);
			$payment["cd_project"] = $last_project;
			$this->load->model('Payments');
			$this->Payments->save($payment);
			redirect(base_url()."project/view/".$last_project);
		}
		else
		{
			$last_project = $this->ProjectModel->save($config);
			redirect(base_url()."project/view/".$last_project);
		}
	}
	
	public function view($id){
		$this->load->helper('date');
		
		$this->db->select('id_project, cd_user, nm_style, cd_file_type, nm_project, vl_value, ds_description, nm_audience, dt_begin, dt_end, fl_identification, nm_user, nm_status');
		$this->db->from('projects');
		$this->db->join('style', 'projects.cd_style = style.id_style');
		$this->db->join('users', 'projects.cd_user = users.id_user');
		$this->db->join('payments', 'projects.id_project = payments.cd_project');
		$this->db->where('projects.id_project', $id);
		
		$resultset = $this->db->get();
		
		/*echo "<pre>";
		echo $this->db->last_query();
		echo "</pre>";*/
		
		$data['id_project'] = $id;
		$data['query'] = $resultset;
		foreach ($resultset->result() as $row){
			if($row->nm_status == 'Completed'){
				$data['page'] = 'pages/project';
			}else{
				$data['page'] = 'pages/waiting';
			}
		}
		
		$this->load->view('mainview', $data);
	}
	
	public function do_upload()
	{
		$this->load->helper('date');
		$this->filename = rand(1,999999).'.jpg';
		
		$config['file_name'] = $this->filename;
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'jpg|png';
		$config['max_size']	= '10000';
	
		$this->load->library('upload', $config);
	
		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
			var_dump($error);
			//$this->load->view('upload_form', $error);
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());
			//echo 'Enviado com sucesso!';
			
			$config['image_library'] = 'gd2';
			$config['source_image']	= './uploads/'.$this->filename;
			$config['create_thumb'] = FALSE;
			$config['maintain_ratio'] = FALSE;
			$config['width'] = 300;
			$config['height'] = 300;
			$this->load->library('image_lib', $config);
			if ( ! $this->image_lib->resize())
			{
			    echo $this->image_lib->display_errors();
			} else {
				//echo "redimensionado com sucesso!";
			}
			
			
			//$this->load->view('upload_success', $data);
			
			$this->load->model('Job');
			$entry['designer'] = $this->session->userdata('id');
			$entry['project'] = $this->input->post('project');
			$entry['urlimage'] = 'uploads/'.$this->filename;
			$entry['description'] = $this->input->post('description');
			$this->Job->save($entry);
			
			redirect('project/view/'.$this->input->post('project'));
		}	
	}
	
	public function get_messages($cd_job){
		$this->db->select('messages.id_message, messages.ds_message, users.nm_user, users.id_user');
		$this->db->from('messages');
		$this->db->join('users', 'messages.cd_user = users.id_user');
		$this->db->where('messages.cd_job', $cd_job);
		$this->db->order_by('messages.id_message', 'asc');
		
		
		$query = $this->db->get();
		
		$class='';
		foreach ($query->result() as $row){
			if($row->id_user == $this->session->userdata('id')) {
				echo '<div class="alert alert-block alert-info">';
				echo '<a class="close" data-dismiss="alert" href="#"><i class="icon icon-remove"></i></a>';
			}else{
				echo '<div class="alert alert-block alert-success">';
			}
			echo '<h4><i class="icon icon-user"></i> '.$row->nm_user .':</h4> '.$row->ds_message;
			echo '</div>';
		}
		
	}
	
	public function send_message($cd_job){
		$this->load->helper('date');
		$data = array(
				'ds_message' => $email = $this->input->post ('message'),
				'cd_job' => $cd_job ,
				'cd_user' => $this->session->userdata('id'),
				'dt_message' => substr(unix_to_human(now()), 0, -3)
		);
		
		$this->db->insert('messages', $data);
		$this->get_messages($cd_job);
	}
	
	public function rate($job, $rate){
		$this->load->helper('date');
		
		$data = array(
			'vl_stars' => $rate,
        );

		$this->db->where('id_job', $job);
		$this->db->update('jobs', $data); 
	}
	
}

